package com.ztjava.upload.controller;

import java.io.File;

import com.jfinal.aop.Duang;
import com.jfinal.kit.Ret;
import com.jfinal.upload.UploadFile;
import com.ztjava.core.Constant;
import com.ztjava.core.base.BaseController;
import com.ztjava.core.tool.UploadTool;
import com.ztjava.ext.auth.annotation.UserMethod;
import com.ztjava.upload.bean.UploadBean;
import com.ztjava.upload.model.UploadFiles;
import com.ztjava.upload.service.UploadService;

public class UploadBaseController extends BaseController{
	private static UploadService uploadService = Duang.duang(UploadService.class);

	public void removeFiles(){
		String upload_token = getPara("upload_token");
		uploadService.removeFiles(upload_token, getParaToInt("id"));
		renderSuccess();
	}
	/***
	 * 构建表单上传文件
	 */
	public void addFilesByForm(){
		UploadFile uploadFile;
		try{
			uploadFile = getFile("files");
		}catch (Exception e){
			renderError(e.getMessage());
			return;
		}
		String upload_token = getPara("upload_token");
		String filename = uploadFile.getFileName();
		String ext = UploadTool.getFileExt(filename);
		long len = uploadFile.getFile().length();
		//-----------------------
		UploadBean uploadBean = uploadService.getFilesToken(upload_token);
		Ret checkResult = uploadService.checkUploadToken(uploadBean, ext, len);
		if(checkResult.getStr(Constant.RESULT).equals(Constant.ERROR)){
			uploadFile.getFile().delete();//不符合规则，则删除文件
			renderError(checkResult.getStr(Constant.MESSAGE));
			return;
		}
		Ret path = uploadService.getFilePath(uploadBean,  ext);
		//-----------------------
		
		uploadFile.getFile().renameTo(new File(path.getStr("folder")));
		
		 //-------------------------------------------------------------------
		UploadFiles uploadFiles= uploadService.insertFiles(uploadBean,path,filename,ext,len);
		setAttr(Constant.RESULT, Constant.SUCCESS);
		setAttr("url", uploadFiles.getStr("local_address"));
		if(uploadBean.getIs_yun().equals("T")&&uploadFiles.get("cloud_url")!=null){
			setAttr("url", uploadFiles.getStr("cloud_url"));
		}
		setAttr("id", uploadFiles.getInt("id"));
		renderJson(new String[]{Constant.RESULT,"url","id"});
	}
	
	/***
	 * base64上传图片
	 * 客户端可以先压缩，再上传，如不需要，建议使用addFilesByForm
	 */
	@UserMethod
	public void addBase64ImgFiles(){
		String token = getPara("token");
		String filename = getPara("filename","unknow");
		String files = getPara("files");
		String ext ="."+files.split(";")[0].split("/")[1];
		long len = files.length();
		len = len-(len/8l)*2l;
		//-----------------------
		UploadBean uploadBean = uploadService.getFilesToken(token);
		Ret checkResult = uploadService.checkUploadToken(uploadBean, ext, len);
		if(checkResult.getStr(Constant.RESULT).equals(Constant.ERROR)){
			renderError(checkResult.getStr(Constant.MESSAGE));
			return;
		}
		Ret path = uploadService.getFilePath(uploadBean, ext);
		//-------------------------
		
		UploadTool.base64ToImg(files, path.getStr("folder"));
		
        //-------------------------------------------------------------------
		UploadFiles uploadFiles= uploadService.insertFiles(uploadBean,path,filename,ext,len);
		setAttr(Constant.RESULT, Constant.SUCCESS);
		setAttr("url", uploadFiles.getStr("local_address"));
		if(uploadBean.getIs_yun().equals("T")&&uploadFiles.get("cloud_url")!=null){
			setAttr("url", uploadFiles.getStr("cloud_url"));
		}
		setAttr("id", uploadFiles.getInt("id"));
		renderJson(new String[]{Constant.RESULT,"url","id"});
	}
}
